package com.myapp.mobilesafe.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import com.myapp.mobilesafe.entity.AppInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lijie on 2016/5/9.
 */
public class AppInfoUtils {
    public static ArrayList<AppInfo> getInstalledApps(Context ctx){
        PackageManager pm = ctx.getPackageManager();
        ArrayList<AppInfo> list = new ArrayList<>();
        List<PackageInfo> installedPackage = pm.getInstalledPackages(0);
        for (PackageInfo p:installedPackage) {
            AppInfo app = new AppInfo();
            String pName = p.packageName;
            ApplicationInfo appInfo = p.applicationInfo;
            String aName = appInfo.loadLabel(pm).toString();
            Drawable icon = appInfo.loadIcon(pm);
            int uid = appInfo.uid;
            app.packageName = pName;
            app.name = aName+" "+uid;
            app.icon = icon;
            int flags = appInfo.flags;// 获取应用标记
            if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE) {
                // 安装在sdcard
                app.isRom = false;
            } else {
                // 安装在手机内存
                app.isRom = true;
            }
            if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
                // 系统应用
                app.isUser = false;
            } else {
                // 用户应用
                app.isUser = true;
            }
            list.add(app);
        }
        return list;
    }
}
